{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.5 \u53cd\u5411\u8fed\u4ee3\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u53cd\u65b9\u5411\u8fed\u4ee3\u4e00\u4e2a\u5e8f\u5217"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u5185\u7f6e\u7684 reversed() \u51fd\u6570\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = [1, 2, 3, 4]\nfor x in reversed(a):\n    print(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53cd\u5411\u8fed\u4ee3\u4ec5\u4ec5\u5f53\u5bf9\u8c61\u7684\u5927\u5c0f\u53ef\u9884\u5148\u786e\u5b9a\u6216\u8005\u5bf9\u8c61\u5b9e\u73b0\u4e86 __reversed__() \u7684\u7279\u6b8a\u65b9\u6cd5\u65f6\u624d\u80fd\u751f\u6548\u3002\n\u5982\u679c\u4e24\u8005\u90fd\u4e0d\u7b26\u5408\uff0c\u90a3\u4f60\u5fc5\u987b\u5148\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u624d\u884c\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Print a file backwards\nf = open('somefile')\nfor line in reversed(list(f)):\n    print(line, end='')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5143\u7d20\u5f88\u591a\u7684\u8bdd\uff0c\u5c06\u5176\u9884\u5148\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u8981\u6d88\u8017\u5927\u91cf\u7684\u5185\u5b58\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u7a0b\u5e8f\u5458\u5e76\u4e0d\u77e5\u9053\u53ef\u4ee5\u901a\u8fc7\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e0a\u5b9e\u73b0 __reversed__() \u65b9\u6cd5\u6765\u5b9e\u73b0\u53cd\u5411\u8fed\u4ee3\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Countdown:\n    def __init__(self, start):\n        self.start = start\n\n    # Forward iterator\n    def __iter__(self):\n        n = self.start\n        while n > 0:\n            yield n\n            n -= 1\n\n    # Reverse iterator\n    def __reversed__(self):\n        n = 1\n        while n <= self.start:\n            yield n\n            n += 1\n\nfor rr in reversed(Countdown(30)):\n    print(rr)\nfor rr in Countdown(30):\n    print(rr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9a\u4e49\u4e00\u4e2a\u53cd\u5411\u8fed\u4ee3\u5668\u53ef\u4ee5\u4f7f\u5f97\u4ee3\u7801\u975e\u5e38\u7684\u9ad8\u6548\uff0c\n\u56e0\u4e3a\u5b83\u4e0d\u518d\u9700\u8981\u5c06\u6570\u636e\u586b\u5145\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u7136\u540e\u518d\u53bb\u53cd\u5411\u8fed\u4ee3\u8fd9\u4e2a\u5217\u8868\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}